<template>
  <div class="app-container">
    <!-- height="300"固定表头，border显示竖直边框 ,stripe显示斑马纹 -->
    <div class="wx-user-box">
      <div class="wx-user-title">微信配置信息列表</div>
      <div class="search-box">
        <el-form :inline="true" :model="formInline" style="margin-left:20px;">
          <el-form-item label="应用名称">
            <el-input v-model="formInline.name" clearable="" placeholder="请输入应用名称" style="width:300px" @change="search" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="margin-left:10px;" @click="search"  icon="el-icon-search">查询</el-button>
            <el-button type="warning" icon="el-icon-circle-plus-outline" @click="addConfig" >添加配置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="wx-user-table-div">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="id" label="ID" width="70" />
          <el-table-column prop="app_title" label="应用名称" width="140" />
          <el-table-column prop="app_id" label="应用ID" width="180" />
          <el-table-column prop="app_secret" label="应用秘钥" width="280" />
          <el-table-column prop="token" label="token值" width="280" />
          <el-table-column label="消息加密" width="80">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.message_encrypt" size="mini" effect="dark">{{ scope.row.message_encrypt }}</el-tag>
              <el-tag v-else type="warning" size="mini" effect="dark">{{ scope.row.message_encrypt }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createtime" label="最后更新时间" />

          <!-- 自定义显示 -->
          <el-table-column>
            <template slot-scope="scope">
              <el-button size="small" icon="el-icon-edit" @click="handleEdit(scope.row.id)">编辑</el-button>
              <el-button v-if="scope.row.id!=1" type="danger" size="small" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="wx-user-pagination-div">
        <el-pagination
          background
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :pager-count="pageCount"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import waves from '@/directive/waves' // 水波纹指令
import { appConfig, delConfig } from '@/api/wx'

export default {
  directives: { waves },
  data() {
    return {
      tableData: [], // 列表数据
      pageSize: 10, // 每页条数
      pageCount: 7, //
      pageSizes: [10, 20, 30, 40, 50], // 个数选择器
      currentPage: 1, // 当前页码
      total: 0, // 总条数
      formInline: {
        name: '' // 应用名称
      }
    }
  },
  computed: {},
  mounted() {
    this.getData(this.currentPage, this.pageSize)
  },
  methods: {

    // 每页显示的条数
    handleSizeChange(val) {
      this.pageSize = val
      this.getData(1, val)
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.currentPage = 1
    },

    // 显示第几页
    handleCurrentChange(val) {
      this.currentPage = val
      // 切换页码时，要获取每页显示的条数
      this.getData(val, this.pageSize)
    },

    // 获取微信配置列表
    getData(page, size) {
      const param = {
        'app_title': this.formInline.name,
        'pageNumber': page,
        'pageSize': size
      }
      // console.log(param)
      appConfig(param).then(res => {
        // console.log(res)
        if (res.state) {
          this.tableData = res.data.list // 数据
          this.total = res.data.totalRow // 总页码
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 搜索
    search() {
      this.getData(this.currentPage, this.pageSize)
    },

    // 添加配置
    addConfig() {
      this.$router.push({ path: '/addConfig/index' })
    },

    // 编辑
    handleEdit(id) {
      this.$router.push({ path: '/updConfig/index', query: { id: id }})
    },

    // 删除
    handleDelete(id) {
      // console.log(id)
      this.$confirm('你是否确定删除这条信息？', '询问', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delConfig({ id: id }).then(res => {
          if (res.state) {
            this.$message({ type: 'success', message: res.msg, duration: 2000 })
            this.getData(this.currentPage, this.pageSize)
          } else {
            this.$message.error(res.msg)
          }
        // console.log(res)
        })
      }).catch(() => {})
    }

  }
}
</script>

<style lang="scss" scoped>
.wx-user-box{
  border: 1px solid #f2f2f2;
  border-radius: 3px;
  box-shadow: 0 1px 1px 0 rgba(0,0,0,.12);
  background: #fff;

  .wx-user-title{
    font-size: 16px;
    color: #333;
    padding: 15px;
    border-bottom: 1px solid #f2f2f2;
  }

  .search-box{
    padding: 20px 15px 0 15px;
  }

  .wx-user-table-div{
    padding: 15px;
  }

  .wx-user-pagination-div{
     padding: 10px 0;
     border-top: 1px solid #dfe6ec;
     margin: 0 15px;
  }

}
</style>

<style lang="scss">
.search-box{
   label{
    font-weight: 500;
  }
  .el-form-item__label{
    font-size: 15px;
    color: #333;
  }
}
</style>
